


/* IV Heterogenity analysis by subgroups */




cd  "G:\Data\Workdata\707677\FKYY7677\Debt_relief\Statafiles\Outputs\Revision\FINAL"


use "G:\Data\Workdata\707677\Common\01Data\02STATA\panel_iv", clear 




keep if time==1

tab obs_lawyer

egen lawyer_id_unique =group(lawyerid court) // Unique lawyer id within courts. some lawyers work in multiple courts
bys lawyer_id_unique: egen n_lawyer=count(lawyer_id_unique)
egen mean_lawyer_leniency= mean(granted), by(lawyer_id_unique)

gen iv_judge2_all = (mean_lawyer_leniency*n_lawyer - granted)/(n_lawyer-1)

gen iv_lawyer2=iv_judge2_all-(iv_court-granted)/(obs_court-1)



keep if n_lawyer>=20

egen n_judge=nvals(lawyer_id_unique), by(court aar)


tab n_judge

keep if n_judge>1

keep pnr iv_lawyer2 lawyer_id_unique


merge 1:m pnr using "G:\Data\Workdata\707677\Common\01Data\02STATA\panel_iv"


keep if _merge==3
drop _merge


keep if time>=1 & time<=16

tab court, gen(courtdum)

tab time, gen(timedum)


egen court_pyear =group(court pyear) 
tab court_pyear

tab aar, gen(aardum)
drop aardum1




gen recession=0
replace recession=1 if pyear>1990 & pyear<1996 
replace recession=1 if pyear==1984
replace recession=1 if pyear==1985



bys recession: sum unemployed_pstill_m1

preserve 

keep if recession==1



local Xvar_short "male age_0_40 age_41_50 age_51_60 age_61_70 erhvervsindk_mean employed_pstill_mean unemployed_pstill_mean legmarried_m1 racedum1 housedum1 passiv_m1 aktiv_m1 higher_secondary university social_mean edu_missing housing_missing timelon_dum1 timelon_dum2 timelon_dum3 timelon_dum4 house_m1"



/* IV Regress */


// Complier mean "Family welfare culture style" NEW


reg granted iv_lawyer2 `Xvar_short' i.court_pyear , cluster(lawyer_id_unique)

reg iv_lawyer2 i.court_pyear if granted==0
predict lawyer_residual, res

reg granted lawyer_residual  i.court_pyear , cluster(lawyer_id_unique)


gen beta_lawyer2=_b[lawyer_residual]

gen alpha_lawyer2=_b[_cons]

sort aar pnr

// Percentiles of instrument

egen p99_iv_lawyer2=pctile(iv_lawyer2), p(99)

egen p1_iv_lawyer2=pctile(iv_lawyer2), p(1)

egen p99_iv_judge2=pctile(iv_lawyer2), p(99)

egen p1_iv_judge2=pctile(iv_lawyer2), p(1)


// IV_lawyer_2

gen complier_iv_lawyer2= beta_lawyer2*(p99_iv_lawyer2-p1_iv_lawyer2) in 1 

gen always_taker_lawyer2 = alpha_lawyer2 + beta_lawyer2 * p1_iv_lawyer2 in 1

gen never_taker_lawyer2 = 1 - alpha_lawyer2 - beta_lawyer2 * p99_iv_lawyer2 in 1

gen all_lawyer2 = complier_iv_lawyer2 + always_taker_lawyer2 + never_taker_lawyer2 in 1

capture drop lawyer_residual

reg iv_lawyer2 i.court_pyear if granted==0
predict lawyer_residual, res

reg erhvervsindk_real lawyer_residual if granted==0

capture drop upper_bound
capture drop lower_bound

gen upper_bound = _b[_cons]+_b[lawyer_residual]*p99_iv_lawyer2
gen lower_bound = _b[_cons] + _b[lawyer_residual]*p1_iv_lawyer2


capture drop complier_mean_mogstad
local complier_mean_mogstad = ((never_taker_lawyer2+ complier_iv_lawyer2)/complier_iv_lawyer2)*(lower_bound)-(never_taker_lawyer2/complier_iv_lawyer2)*upper_bound




// END NEW


// complier means for non-granted

gen no_treat=(1-granted)



gen y_no_treat= no_treat*erhvervsindk_real
gen y_treat= granted*erhvervsindk_real


ivreghdfe y_no_treat (no_treat=iv_lawyer2) , absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) // Include controls? 
local complier_mean=_b[no_treat]

ivreghdfe y_treat (granted=iv_lawyer2) , absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 



sum erhvervsindk_real if granted==0

local dismissed_mean=r(mean)



sum erhvervsindk_real 

local dependent_mean=r(mean)



reg   erhvervsindk_real granted `Xvar_short' i.court_pyear if time==1
local individuals=e(N)


ivreghdfe erhvervsindk_real (granted=iv_lawyer2) `Xvar_short' aardum2-aardum35 , absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 






outreg2 using iv_results_recession_8485_9195.xls, replace ctitle(IV erhvervsindk_real) keep(granted) ///
 addstat(Non-granted mean, `dismissed_mean',Non-granted complier mean, `complier_mean', complier mean mogstad, `complier_mean_mogstad', dependent_mean, `dependent_mean', individuals, `individuals')





local depvar "employed_pstill unemployed_pstill out_of_lf_pstill timelon_real aktiv_real passiv_real formue_real koejd_real house tilbtot_real disability kontant_real social netovskud_real dispon_ny_real legmarried attrition mortgage_real"





foreach var of varlist `depvar'{

// complier means for non-granted
capture drop y_no_treat
gen y_no_treat= no_treat*`var'

ivreghdfe y_no_treat (no_treat=iv_lawyer2) , absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 
local complier_mean=_b[no_treat]

// complier mean non-granted mogstad

reg `var' lawyer_residual if granted==0

capture drop upper_bound
capture drop lower_bound

gen upper_bound = _b[_cons]+_b[lawyer_residual]*p99_iv_lawyer2
gen lower_bound = _b[_cons] + _b[lawyer_residual]*p1_iv_lawyer2


capture drop complier_mean_mogstad
local complier_mean_mogstad = ((never_taker_lawyer2+ complier_iv_lawyer2)/complier_iv_lawyer2)*(lower_bound)-(never_taker_lawyer2/complier_iv_lawyer2)*upper_bound



sum `var' if granted==0
local dismissed_mean=r(mean)


sum `var' 
local dependent_mean=r(mean)



reg `var' granted `Xvar_short' i.court_pyear if time==1
local individuals=e(N)

ivreghdfe `var' (granted=iv_lawyer2) `Xvar_short' aardum2-aardum35, absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 


outreg2 using iv_results_recession_8485_9195.xls, append ctitle(IV `var')  keep(granted) ///
 addstat(Non-granted mean, `dismissed_mean',Non-granted complier mean, `complier_mean', complier mean mogstad, `complier_mean_mogstad', dependent_mean, `dependent_mean', individuals, `individuals')



}


* bankgaeld_real


local depvar "bankgaeld_real"





foreach var of varlist `depvar'{

// complier means for non-granted
capture drop y_no_treat
gen y_no_treat= no_treat*`var'

ivreghdfe y_no_treat (no_treat=iv_lawyer2) , absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 
local complier_mean=_b[no_treat]

// complier mean non-granted mogstad

reg `var' lawyer_residual if granted==0

capture drop upper_bound
capture drop lower_bound

gen upper_bound = _b[_cons]+_b[lawyer_residual]*p99_iv_lawyer2
gen lower_bound = _b[_cons] + _b[lawyer_residual]*p1_iv_lawyer2


capture drop complier_mean_mogstad
local complier_mean_mogstad = ((never_taker_lawyer2+ complier_iv_lawyer2)/complier_iv_lawyer2)*(lower_bound)-(never_taker_lawyer2/complier_iv_lawyer2)*upper_bound



sum `var' if granted==0
local dismissed_mean=r(mean)


sum `var' 
local dependent_mean=r(mean)



reg `var' granted `Xvar_short' i.court_pyear if time==1
local individuals=e(N)

ivreghdfe `var' (granted=iv_lawyer2) `Xvar_short' aardum5-aardum35, absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 


outreg2 using iv_results_recession_8485_9195.xls, append ctitle(IV `var')  keep(granted) ///
 addstat(Non-granted mean, `dismissed_mean',Non-granted complier mean, `complier_mean', complier mean mogstad, `complier_mean_mogstad', dependent_mean, `dependent_mean', individuals, `individuals')



}





restore 

preserve 

keep if recession==0



local Xvar_short "male age_0_40 age_41_50 age_51_60 age_61_70 erhvervsindk_mean employed_pstill_mean unemployed_pstill_mean legmarried_m1 racedum1 housedum1 passiv_m1 aktiv_m1 higher_secondary university social_mean edu_missing housing_missing timelon_dum1 timelon_dum2 timelon_dum3 timelon_dum4 house_m1"



/* IV Regress */


// Complier mean "Family welfare culture style" NEW


reg granted iv_lawyer2 `Xvar_short' i.court_pyear , cluster(lawyer_id_unique)

reg iv_lawyer2 i.court_pyear if granted==0
predict lawyer_residual, res

reg granted lawyer_residual  i.court_pyear , cluster(lawyer_id_unique)


gen beta_lawyer2=_b[lawyer_residual]

gen alpha_lawyer2=_b[_cons]

sort aar pnr

// Percentiles of instrument

egen p99_iv_lawyer2=pctile(iv_lawyer2), p(99)

egen p1_iv_lawyer2=pctile(iv_lawyer2), p(1)

egen p99_iv_judge2=pctile(iv_lawyer2), p(99)

egen p1_iv_judge2=pctile(iv_lawyer2), p(1)


// IV_lawyer_2

gen complier_iv_lawyer2= beta_lawyer2*(p99_iv_lawyer2-p1_iv_lawyer2) in 1 

gen always_taker_lawyer2 = alpha_lawyer2 + beta_lawyer2 * p1_iv_lawyer2 in 1

gen never_taker_lawyer2 = 1 - alpha_lawyer2 - beta_lawyer2 * p99_iv_lawyer2 in 1

gen all_lawyer2 = complier_iv_lawyer2 + always_taker_lawyer2 + never_taker_lawyer2 in 1

capture drop lawyer_residual

reg iv_lawyer2 i.court_pyear if granted==0
predict lawyer_residual, res

reg erhvervsindk_real lawyer_residual if granted==0

capture drop upper_bound
capture drop lower_bound

gen upper_bound = _b[_cons]+_b[lawyer_residual]*p99_iv_lawyer2
gen lower_bound = _b[_cons] + _b[lawyer_residual]*p1_iv_lawyer2


capture drop complier_mean_mogstad
local complier_mean_mogstad = ((never_taker_lawyer2+ complier_iv_lawyer2)/complier_iv_lawyer2)*(lower_bound)-(never_taker_lawyer2/complier_iv_lawyer2)*upper_bound




// END NEW




// complier means for non-granted

gen no_treat=(1-granted)



gen y_no_treat= no_treat*erhvervsindk_real
gen y_treat= granted*erhvervsindk_real


ivreghdfe y_no_treat (no_treat=iv_lawyer2) , absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) // Include controls? 
local complier_mean=_b[no_treat]

ivreghdfe y_treat (granted=iv_lawyer2) , absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 



sum erhvervsindk_real if granted==0

local dismissed_mean=r(mean)



sum erhvervsindk_real 

local dependent_mean=r(mean)




reg   erhvervsindk_real granted `Xvar_short' i.court_pyear if time==1
local individuals=e(N)



ivreghdfe erhvervsindk_real (granted=iv_lawyer2) `Xvar_short' aardum4-aardum35, absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 





outreg2 using iv_results_non_recession_8485_9195.xls, replace ctitle(IV erhvervsindk_real) keep(granted) ///
 addstat(Non-granted mean, `dismissed_mean',Non-granted complier mean, `complier_mean', complier mean mogstad, `complier_mean_mogstad', dependent_mean, `dependent_mean', individuals, `individuals')




local depvar "employed_pstill unemployed_pstill out_of_lf_pstill timelon_real aktiv_real passiv_real formue_real koejd_real house tilbtot_real disability kontant_real social netovskud_real dispon_ny_real legmarried attrition mortgage_real"





foreach var of varlist `depvar'{

// complier means for non-granted
capture drop y_no_treat
gen y_no_treat= no_treat*`var'

ivreghdfe y_no_treat (no_treat=iv_lawyer2) , absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 
local complier_mean=_b[no_treat]

// complier mean non-granted mogstad

reg `var' lawyer_residual if granted==0

capture drop upper_bound
capture drop lower_bound

gen upper_bound = _b[_cons]+_b[lawyer_residual]*p99_iv_lawyer2
gen lower_bound = _b[_cons] + _b[lawyer_residual]*p1_iv_lawyer2


capture drop complier_mean_mogstad
local complier_mean_mogstad = ((never_taker_lawyer2+ complier_iv_lawyer2)/complier_iv_lawyer2)*(lower_bound)-(never_taker_lawyer2/complier_iv_lawyer2)*upper_bound



sum `var' if granted==0
local dismissed_mean=r(mean)


sum `var' 
local dependent_mean=r(mean)



reg `var' granted `Xvar_short' i.court_pyear if time==1
local individuals=e(N)

ivreghdfe `var' (granted=iv_lawyer2) `Xvar_short' aardum4-aardum35, absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 


outreg2 using iv_results_non_recession_8485_9195.xls, append ctitle(IV `var')  keep(granted) ///
 addstat(Non-granted mean, `dismissed_mean',Non-granted complier mean, `complier_mean', complier mean mogstad, `complier_mean_mogstad', dependent_mean, `dependent_mean', individuals, `individuals')



}






local depvar "bankgaeld_real"





foreach var of varlist `depvar'{

// complier means for non-granted
capture drop y_no_treat
gen y_no_treat= no_treat*`var'

ivreghdfe y_no_treat (no_treat=iv_lawyer2) , absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 
local complier_mean=_b[no_treat]

// complier mean non-granted mogstad

reg `var' lawyer_residual if granted==0

capture drop upper_bound
capture drop lower_bound

gen upper_bound = _b[_cons]+_b[lawyer_residual]*p99_iv_lawyer2
gen lower_bound = _b[_cons] + _b[lawyer_residual]*p1_iv_lawyer2


capture drop complier_mean_mogstad
local complier_mean_mogstad = ((never_taker_lawyer2+ complier_iv_lawyer2)/complier_iv_lawyer2)*(lower_bound)-(never_taker_lawyer2/complier_iv_lawyer2)*upper_bound



sum `var' if granted==0
local dismissed_mean=r(mean)


sum `var' 
local dependent_mean=r(mean)



reg `var' granted `Xvar_short' i.court_pyear if time==1
local individuals=e(N)

ivreghdfe `var' (granted=iv_lawyer2) `Xvar_short' aardum4-aardum35, absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 


outreg2 using iv_results_non_recession_8485_9195.xls, append ctitle(IV `var')  keep(granted) ///
 addstat(Non-granted mean, `dismissed_mean',Non-granted complier mean, `complier_mean', complier mean mogstad, `complier_mean_mogstad', dependent_mean, `dependent_mean', individuals, `individuals')



}




restore 






* employed(-2,-3)





use "G:\Data\Workdata\707677\Common\01Data\02STATA\panel_iv", clear 




keep if time==1

tab obs_lawyer

egen lawyer_id_unique =group(lawyerid court) // Unique lawyer id within courts. some lawyers work in multiple courts
bys lawyer_id_unique: egen n_lawyer=count(lawyer_id_unique)
egen mean_lawyer_leniency= mean(granted), by(lawyer_id_unique)

gen iv_judge2_all = (mean_lawyer_leniency*n_lawyer - granted)/(n_lawyer-1)

gen iv_lawyer2=iv_judge2_all-(iv_court-granted)/(obs_court-1)

* Data restrictions more than 20 obs pr lawyer and multiple lawyers in a given court*year



keep if n_lawyer>=20

egen n_judge=nvals(lawyer_id_unique), by(court aar)


tab n_judge

keep if n_judge>1

keep pnr iv_lawyer2 lawyer_id_unique


merge 1:m pnr using "G:\Data\Workdata\707677\Common\01Data\02STATA\panel_iv"


keep if _merge==3
drop _merge


keep if time>=1 & time<=16

tab court, gen(courtdum)

tab time, gen(timedum)


egen court_pyear =group(court pyear) 
tab court_pyear

tab aar, gen(aardum)
drop aardum1


gen employed_m2_m3=.
replace employed_m2_m3=1 if employed_pstill_m2+employed_pstill_m3>0 &  employed_pstill_m2+employed_pstill_m3!=.
replace employed_m2_m3=0 if employed_pstill_m2+employed_pstill_m3==0 &  employed_pstill_m2+employed_pstill_m3!=.




* 2 above median 1 below median


preserve 

keep if employed_m2_m3==1



local Xvar_short "male age_0_40 age_41_50 age_51_60 age_61_70 erhvervsindk_mean employed_pstill_mean unemployed_pstill_mean legmarried_m1 racedum1 housedum1 passiv_m1 aktiv_m1 higher_secondary university social_mean edu_missing housing_missing timelon_dum1 timelon_dum2 timelon_dum3 timelon_dum4 house_m1"



/* IV Regress */


// Complier mean "Family welfare culture style" NEW


reg granted iv_lawyer2 `Xvar_short' i.court_pyear , cluster(lawyer_id_unique)

reg iv_lawyer2 i.court_pyear if granted==0
predict lawyer_residual, res

reg granted lawyer_residual  i.court_pyear , cluster(lawyer_id_unique)


gen beta_lawyer2=_b[lawyer_residual]

gen alpha_lawyer2=_b[_cons]

sort aar pnr

// Percentiles of instrument

egen p99_iv_lawyer2=pctile(iv_lawyer2), p(99)

egen p1_iv_lawyer2=pctile(iv_lawyer2), p(1)

egen p99_iv_judge2=pctile(iv_lawyer2), p(99)

egen p1_iv_judge2=pctile(iv_lawyer2), p(1)


// IV_lawyer_2

gen complier_iv_lawyer2= beta_lawyer2*(p99_iv_lawyer2-p1_iv_lawyer2) in 1 

gen always_taker_lawyer2 = alpha_lawyer2 + beta_lawyer2 * p1_iv_lawyer2 in 1

gen never_taker_lawyer2 = 1 - alpha_lawyer2 - beta_lawyer2 * p99_iv_lawyer2 in 1

gen all_lawyer2 = complier_iv_lawyer2 + always_taker_lawyer2 + never_taker_lawyer2 in 1

capture drop lawyer_residual

reg iv_lawyer2 i.court_pyear if granted==0
predict lawyer_residual, res

reg erhvervsindk_real lawyer_residual if granted==0

capture drop upper_bound
capture drop lower_bound

gen upper_bound = _b[_cons]+_b[lawyer_residual]*p99_iv_lawyer2
gen lower_bound = _b[_cons] + _b[lawyer_residual]*p1_iv_lawyer2


capture drop complier_mean_mogstad
local complier_mean_mogstad = ((never_taker_lawyer2+ complier_iv_lawyer2)/complier_iv_lawyer2)*(lower_bound)-(never_taker_lawyer2/complier_iv_lawyer2)*upper_bound




// END NEW



// complier means for non-granted

gen no_treat=(1-granted)



gen y_no_treat= no_treat*erhvervsindk_real
gen y_treat= granted*erhvervsindk_real


ivreghdfe y_no_treat (no_treat=iv_lawyer2) , absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) // Include controls? 
local complier_mean=_b[no_treat]

ivreghdfe y_treat (granted=iv_lawyer2) , absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 



sum erhvervsindk_real if granted==0

local dismissed_mean=r(mean)



sum erhvervsindk_real 

local dependent_mean=r(mean)



reg   erhvervsindk_real granted `Xvar_short' i.court_pyear if time==1
local individuals=e(N)



ivreghdfe erhvervsindk_real (granted=iv_lawyer2) `Xvar_short'  aardum2-aardum35, absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 






outreg2 using iv_results_employed_prior.xls, replace ctitle(IV erhvervsindk_real) keep(granted) ///
 addstat(Non-granted mean, `dismissed_mean',Non-granted complier mean, `complier_mean', complier mean mogstad, `complier_mean_mogstad', dependent_mean, `dependent_mean', individuals, `individuals')



local depvar "employed_pstill unemployed_pstill out_of_lf_pstill timelon_real aktiv_real passiv_real formue_real koejd_real house tilbtot_real disability kontant_real social netovskud_real dispon_ny_real legmarried attrition mortgage_real"





foreach var of varlist `depvar'{

// complier means for non-granted
capture drop y_no_treat
gen y_no_treat= no_treat*`var'

ivreghdfe y_no_treat (no_treat=iv_lawyer2) , absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 
local complier_mean=_b[no_treat]

// complier mean non-granted mogstad

reg `var' lawyer_residual if granted==0

capture drop upper_bound
capture drop lower_bound

gen upper_bound = _b[_cons]+_b[lawyer_residual]*p99_iv_lawyer2
gen lower_bound = _b[_cons] + _b[lawyer_residual]*p1_iv_lawyer2


capture drop complier_mean_mogstad
local complier_mean_mogstad = ((never_taker_lawyer2+ complier_iv_lawyer2)/complier_iv_lawyer2)*(lower_bound)-(never_taker_lawyer2/complier_iv_lawyer2)*upper_bound



sum `var' if granted==0
local dismissed_mean=r(mean)


sum `var' 
local dependent_mean=r(mean)



reg `var' granted `Xvar_short' i.court_pyear if time==1
local individuals=e(N)

ivreghdfe `var' (granted=iv_lawyer2) `Xvar_short'  aardum2-aardum35, absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 


outreg2 using iv_results_employed_prior.xls, append ctitle(IV `var')  keep(granted) ///
 addstat(Non-granted mean, `dismissed_mean',Non-granted complier mean, `complier_mean', complier mean mogstad, `complier_mean_mogstad', dependent_mean, `dependent_mean', individuals, `individuals')



}


local depvar "bankgaeld_real"





foreach var of varlist `depvar'{

// complier means for non-granted
capture drop y_no_treat
gen y_no_treat= no_treat*`var'

ivreghdfe y_no_treat (no_treat=iv_lawyer2) , absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 
local complier_mean=_b[no_treat]

// complier mean non-granted mogstad

reg `var' lawyer_residual if granted==0

capture drop upper_bound
capture drop lower_bound

gen upper_bound = _b[_cons]+_b[lawyer_residual]*p99_iv_lawyer2
gen lower_bound = _b[_cons] + _b[lawyer_residual]*p1_iv_lawyer2


capture drop complier_mean_mogstad
local complier_mean_mogstad = ((never_taker_lawyer2+ complier_iv_lawyer2)/complier_iv_lawyer2)*(lower_bound)-(never_taker_lawyer2/complier_iv_lawyer2)*upper_bound



sum `var' if granted==0
local dismissed_mean=r(mean)


sum `var' 
local dependent_mean=r(mean)



reg `var' granted `Xvar_short' i.court_pyear if time==1
local individuals=e(N)

ivreghdfe `var' (granted=iv_lawyer2) `Xvar_short'  aardum5-aardum35, absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 


outreg2 using iv_results_employed_prior.xls, append ctitle(IV `var')  keep(granted) ///
 addstat(Non-granted mean, `dismissed_mean',Non-granted complier mean, `complier_mean', complier mean mogstad, `complier_mean_mogstad', dependent_mean, `dependent_mean', individuals, `individuals')



}



restore 




preserve 

keep if employed_m2_m3==0



local Xvar_short "male age_0_40 age_41_50 age_51_60 age_61_70 erhvervsindk_mean employed_pstill_mean unemployed_pstill_mean legmarried_m1 racedum1 housedum1 passiv_m1 aktiv_m1 higher_secondary university social_mean edu_missing housing_missing timelon_dum1 timelon_dum2 timelon_dum3 timelon_dum4 house_m1"



/* IV Regress */


// Complier mean "Family welfare culture style" NEW


reg granted iv_lawyer2 `Xvar_short' i.court_pyear , cluster(lawyer_id_unique)

reg iv_lawyer2 i.court_pyear if granted==0
predict lawyer_residual, res

reg granted lawyer_residual  i.court_pyear , cluster(lawyer_id_unique)


gen beta_lawyer2=_b[lawyer_residual]

gen alpha_lawyer2=_b[_cons]

sort aar pnr

// Percentiles of instrument

egen p99_iv_lawyer2=pctile(iv_lawyer2), p(99)

egen p1_iv_lawyer2=pctile(iv_lawyer2), p(1)

egen p99_iv_judge2=pctile(iv_lawyer2), p(99)

egen p1_iv_judge2=pctile(iv_lawyer2), p(1)


// IV_lawyer_2

gen complier_iv_lawyer2= beta_lawyer2*(p99_iv_lawyer2-p1_iv_lawyer2) in 1 

gen always_taker_lawyer2 = alpha_lawyer2 + beta_lawyer2 * p1_iv_lawyer2 in 1

gen never_taker_lawyer2 = 1 - alpha_lawyer2 - beta_lawyer2 * p99_iv_lawyer2 in 1

gen all_lawyer2 = complier_iv_lawyer2 + always_taker_lawyer2 + never_taker_lawyer2 in 1

capture drop lawyer_residual

reg iv_lawyer2 i.court_pyear if granted==0
predict lawyer_residual, res

reg erhvervsindk_real lawyer_residual if granted==0

capture drop upper_bound
capture drop lower_bound

gen upper_bound = _b[_cons]+_b[lawyer_residual]*p99_iv_lawyer2
gen lower_bound = _b[_cons] + _b[lawyer_residual]*p1_iv_lawyer2


capture drop complier_mean_mogstad
local complier_mean_mogstad = ((never_taker_lawyer2+ complier_iv_lawyer2)/complier_iv_lawyer2)*(lower_bound)-(never_taker_lawyer2/complier_iv_lawyer2)*upper_bound




// END NEW



// complier means for non-granted

gen no_treat=(1-granted)



gen y_no_treat= no_treat*erhvervsindk_real
gen y_treat= granted*erhvervsindk_real


ivreghdfe y_no_treat (no_treat=iv_lawyer2) , absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) // Include controls? 
local complier_mean=_b[no_treat]

ivreghdfe y_treat (granted=iv_lawyer2) , absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 



sum erhvervsindk_real if granted==0

local dismissed_mean=r(mean)


sum erhvervsindk_real 

local dependent_mean=r(mean)




reg   erhvervsindk_real granted `Xvar_short' i.court_pyear if time==1
local individuals=e(N)



ivreghdfe erhvervsindk_real (granted=iv_lawyer2) `Xvar_short'  aardum2-aardum35, absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 





outreg2 using iv_results_not_employed_prior.xls, replace ctitle(IV erhvervsindk_real) keep(granted) ///
 addstat(Non-granted mean, `dismissed_mean',Non-granted complier mean, `complier_mean', complier mean mogstad, `complier_mean_mogstad', dependent_mean, `dependent_mean', individuals, `individuals')




local depvar "employed_pstill unemployed_pstill out_of_lf_pstill timelon_real aktiv_real passiv_real formue_real koejd_real house tilbtot_real disability kontant_real social netovskud_real dispon_ny_real legmarried attrition mortgage_real"





foreach var of varlist `depvar'{

// complier means for non-granted
capture drop y_no_treat
gen y_no_treat= no_treat*`var'

ivreghdfe y_no_treat (no_treat=iv_lawyer2) , absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 
local complier_mean=_b[no_treat]

// complier mean non-granted mogstad

reg `var' lawyer_residual if granted==0

capture drop upper_bound
capture drop lower_bound

gen upper_bound = _b[_cons]+_b[lawyer_residual]*p99_iv_lawyer2
gen lower_bound = _b[_cons] + _b[lawyer_residual]*p1_iv_lawyer2


capture drop complier_mean_mogstad
local complier_mean_mogstad = ((never_taker_lawyer2+ complier_iv_lawyer2)/complier_iv_lawyer2)*(lower_bound)-(never_taker_lawyer2/complier_iv_lawyer2)*upper_bound



sum `var' if granted==0
local dismissed_mean=r(mean)


sum `var' 
local dependent_mean=r(mean)



reg `var' granted `Xvar_short' i.court_pyear if time==1
local individuals=e(N)

ivreghdfe `var' (granted=iv_lawyer2) `Xvar_short'  aardum2-aardum35, absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 


outreg2 using iv_results_not_employed_prior.xls, append ctitle(IV `var')  keep(granted) ///
 addstat(Non-granted mean, `dismissed_mean',Non-granted complier mean, `complier_mean', complier mean mogstad, `complier_mean_mogstad', dependent_mean, `dependent_mean', individuals, `individuals')



}








local depvar "bankgaeld_real"





foreach var of varlist `depvar'{

// complier means for non-granted
capture drop y_no_treat
gen y_no_treat= no_treat*`var'

ivreghdfe y_no_treat (no_treat=iv_lawyer2) , absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 
local complier_mean=_b[no_treat]

// complier mean non-granted mogstad

reg `var' lawyer_residual if granted==0

capture drop upper_bound
capture drop lower_bound

gen upper_bound = _b[_cons]+_b[lawyer_residual]*p99_iv_lawyer2
gen lower_bound = _b[_cons] + _b[lawyer_residual]*p1_iv_lawyer2


capture drop complier_mean_mogstad
local complier_mean_mogstad = ((never_taker_lawyer2+ complier_iv_lawyer2)/complier_iv_lawyer2)*(lower_bound)-(never_taker_lawyer2/complier_iv_lawyer2)*upper_bound



sum `var' if granted==0
local dismissed_mean=r(mean)


sum `var' 
local dependent_mean=r(mean)



reg `var' granted `Xvar_short' i.court_pyear if time==1
local individuals=e(N)

ivreghdfe `var' (granted=iv_lawyer2) `Xvar_short'  aardum5-aardum35, absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 


outreg2 using iv_results_not_employed_prior.xls, append ctitle(IV `var')  keep(granted) ///
 addstat(Non-granted mean, `dismissed_mean',Non-granted complier mean, `complier_mean', complier mean mogstad, `complier_mean_mogstad', dependent_mean, `dependent_mean', individuals, `individuals')



}



restore 



* Wealth


* Abobe/below median wealth(mean 4 years prior)

	egen permant_formue_groups_prior=xtile(formue_mean), nq(2) by(pyear)
order formue_mean permant_formue_groups_prior 

* 2 above median 1 below median


preserve 

keep if permant_formue_groups_prior==2



local Xvar_short "male age_0_40 age_41_50 age_51_60 age_61_70 erhvervsindk_mean employed_pstill_mean unemployed_pstill_mean legmarried_m1 racedum1 housedum1 passiv_m1 aktiv_m1 higher_secondary university social_mean edu_missing housing_missing timelon_dum1 timelon_dum2 timelon_dum3 timelon_dum4 house_m1"



/* IV Regress */


// Complier mean "Family welfare culture style" NEW


reg granted iv_lawyer2 `Xvar_short' i.court_pyear , cluster(lawyer_id_unique)

reg iv_lawyer2 i.court_pyear if granted==0
predict lawyer_residual, res

reg granted lawyer_residual  i.court_pyear , cluster(lawyer_id_unique)


gen beta_lawyer2=_b[lawyer_residual]

gen alpha_lawyer2=_b[_cons]

sort aar pnr

// Percentiles of instrument

egen p99_iv_lawyer2=pctile(iv_lawyer2), p(99)

egen p1_iv_lawyer2=pctile(iv_lawyer2), p(1)

egen p99_iv_judge2=pctile(iv_lawyer2), p(99)

egen p1_iv_judge2=pctile(iv_lawyer2), p(1)


// IV_lawyer_2

gen complier_iv_lawyer2= beta_lawyer2*(p99_iv_lawyer2-p1_iv_lawyer2) in 1 

gen always_taker_lawyer2 = alpha_lawyer2 + beta_lawyer2 * p1_iv_lawyer2 in 1

gen never_taker_lawyer2 = 1 - alpha_lawyer2 - beta_lawyer2 * p99_iv_lawyer2 in 1

gen all_lawyer2 = complier_iv_lawyer2 + always_taker_lawyer2 + never_taker_lawyer2 in 1

capture drop lawyer_residual

reg iv_lawyer2 i.court_pyear if granted==0
predict lawyer_residual, res

reg erhvervsindk_real lawyer_residual if granted==0

capture drop upper_bound
capture drop lower_bound

gen upper_bound = _b[_cons]+_b[lawyer_residual]*p99_iv_lawyer2
gen lower_bound = _b[_cons] + _b[lawyer_residual]*p1_iv_lawyer2


capture drop complier_mean_mogstad
local complier_mean_mogstad = ((never_taker_lawyer2+ complier_iv_lawyer2)/complier_iv_lawyer2)*(lower_bound)-(never_taker_lawyer2/complier_iv_lawyer2)*upper_bound




// END NEW




// complier means for non-granted

gen no_treat=(1-granted)



gen y_no_treat= no_treat*erhvervsindk_real
gen y_treat= granted*erhvervsindk_real


ivreghdfe y_no_treat (no_treat=iv_lawyer2) , absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) // Include controls? 
local complier_mean=_b[no_treat]

ivreghdfe y_treat (granted=iv_lawyer2) , absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 


sum erhvervsindk_real if granted==0

local dismissed_mean=r(mean)



sum erhvervsindk_real 

local dependent_mean=r(mean)



reg   erhvervsindk_real granted `Xvar_short' i.court_pyear if time==1
local individuals=e(N)



ivreghdfe erhvervsindk_real (granted=iv_lawyer2) `Xvar_short' aardum2-aardum35, absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 





outreg2 using iv_results_above_median_wealth.xls, replace ctitle(IV erhvervsindk_real) keep(granted) ///
 addstat(Non-granted mean, `dismissed_mean',Non-granted complier mean, `complier_mean', complier mean mogstad, `complier_mean_mogstad', dependent_mean, `dependent_mean', individuals, `individuals')




local depvar "employed_pstill unemployed_pstill out_of_lf_pstill timelon_real aktiv_real passiv_real formue_real koejd_real house tilbtot_real disability kontant_real social netovskud_real dispon_ny_real legmarried attrition mortgage_real "





foreach var of varlist `depvar'{

// complier means for non-granted
capture drop y_no_treat
gen y_no_treat= no_treat*`var'

ivreghdfe y_no_treat (no_treat=iv_lawyer2) , absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 
local complier_mean=_b[no_treat]

// complier mean non-granted mogstad

reg `var' lawyer_residual if granted==0

capture drop upper_bound
capture drop lower_bound

gen upper_bound = _b[_cons]+_b[lawyer_residual]*p99_iv_lawyer2
gen lower_bound = _b[_cons] + _b[lawyer_residual]*p1_iv_lawyer2


capture drop complier_mean_mogstad
local complier_mean_mogstad = ((never_taker_lawyer2+ complier_iv_lawyer2)/complier_iv_lawyer2)*(lower_bound)-(never_taker_lawyer2/complier_iv_lawyer2)*upper_bound



sum `var' if granted==0
local dismissed_mean=r(mean)


sum `var' 
local dependent_mean=r(mean)



reg `var' granted `Xvar_short' i.court_pyear if time==1
local individuals=e(N)

ivreghdfe `var' (granted=iv_lawyer2) `Xvar_short' aardum2-aardum35, absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 


outreg2 using iv_results_above_median_wealth.xls, append ctitle(IV `var')  keep(granted) ///
 addstat(Non-granted mean, `dismissed_mean',Non-granted complier mean, `complier_mean', complier mean mogstad, `complier_mean_mogstad', dependent_mean, `dependent_mean', individuals, `individuals')



}


local depvar "bankgaeld_real"





foreach var of varlist `depvar'{

// complier means for non-granted
capture drop y_no_treat
gen y_no_treat= no_treat*`var'

ivreghdfe y_no_treat (no_treat=iv_lawyer2) , absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 
local complier_mean=_b[no_treat]

// complier mean non-granted mogstad

reg `var' lawyer_residual if granted==0

capture drop upper_bound
capture drop lower_bound

gen upper_bound = _b[_cons]+_b[lawyer_residual]*p99_iv_lawyer2
gen lower_bound = _b[_cons] + _b[lawyer_residual]*p1_iv_lawyer2


capture drop complier_mean_mogstad
local complier_mean_mogstad = ((never_taker_lawyer2+ complier_iv_lawyer2)/complier_iv_lawyer2)*(lower_bound)-(never_taker_lawyer2/complier_iv_lawyer2)*upper_bound



sum `var' if granted==0
local dismissed_mean=r(mean)


sum `var' 
local dependent_mean=r(mean)



reg `var' granted `Xvar_short' i.court_pyear if time==1
local individuals=e(N)

ivreghdfe `var' (granted=iv_lawyer2) `Xvar_short'  aardum5-aardum35, absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 


outreg2 using iv_results_above_median_wealth.xls, append ctitle(IV `var')  keep(granted) ///
 addstat(Non-granted mean, `dismissed_mean',Non-granted complier mean, `complier_mean', complier mean mogstad, `complier_mean_mogstad', dependent_mean, `dependent_mean', individuals, `individuals')



}



restore 








preserve 

keep if permant_formue_groups_prior==1



local Xvar_short "male age_0_40 age_41_50 age_51_60 age_61_70 erhvervsindk_mean employed_pstill_mean unemployed_pstill_mean legmarried_m1 racedum1 housedum1 passiv_m1 aktiv_m1 higher_secondary university social_mean edu_missing housing_missing timelon_dum1 timelon_dum2 timelon_dum3 timelon_dum4 house_m1"



/* IV Regress */


// Complier mean "Family welfare culture style" NEW


reg granted iv_lawyer2 `Xvar_short' i.court_pyear , cluster(lawyer_id_unique)

reg iv_lawyer2 i.court_pyear if granted==0
predict lawyer_residual, res

reg granted lawyer_residual  i.court_pyear , cluster(lawyer_id_unique)


gen beta_lawyer2=_b[lawyer_residual]

gen alpha_lawyer2=_b[_cons]

sort aar pnr

// Percentiles of instrument

egen p99_iv_lawyer2=pctile(iv_lawyer2), p(99)

egen p1_iv_lawyer2=pctile(iv_lawyer2), p(1)

egen p99_iv_judge2=pctile(iv_lawyer2), p(99)

egen p1_iv_judge2=pctile(iv_lawyer2), p(1)


// IV_lawyer_2

gen complier_iv_lawyer2= beta_lawyer2*(p99_iv_lawyer2-p1_iv_lawyer2) in 1 

gen always_taker_lawyer2 = alpha_lawyer2 + beta_lawyer2 * p1_iv_lawyer2 in 1

gen never_taker_lawyer2 = 1 - alpha_lawyer2 - beta_lawyer2 * p99_iv_lawyer2 in 1

gen all_lawyer2 = complier_iv_lawyer2 + always_taker_lawyer2 + never_taker_lawyer2 in 1

capture drop lawyer_residual

reg iv_lawyer2 i.court_pyear if granted==0
predict lawyer_residual, res

reg erhvervsindk_real lawyer_residual if granted==0

capture drop upper_bound
capture drop lower_bound

gen upper_bound = _b[_cons]+_b[lawyer_residual]*p99_iv_lawyer2
gen lower_bound = _b[_cons] + _b[lawyer_residual]*p1_iv_lawyer2


capture drop complier_mean_mogstad
local complier_mean_mogstad = ((never_taker_lawyer2+ complier_iv_lawyer2)/complier_iv_lawyer2)*(lower_bound)-(never_taker_lawyer2/complier_iv_lawyer2)*upper_bound




// END NEW



// complier means for non-granted

gen no_treat=(1-granted)



gen y_no_treat= no_treat*erhvervsindk_real
gen y_treat= granted*erhvervsindk_real


ivreghdfe y_no_treat (no_treat=iv_lawyer2) , absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) // Include controls? 
local complier_mean=_b[no_treat]

ivreghdfe y_treat (granted=iv_lawyer2) , absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 


sum erhvervsindk_real if granted==0

local dismissed_mean=r(mean)



sum erhvervsindk_real 

local dependent_mean=r(mean)



reg   erhvervsindk_real granted `Xvar_short' i.court_pyear if time==1
local individuals=e(N)



ivreghdfe erhvervsindk_real (granted=iv_lawyer2) `Xvar_short' aardum2-aardum35, absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 




outreg2 using iv_results_below_median_wealth.xls, replace ctitle(IV erhvervsindk_real) keep(granted) ///
 addstat(Non-granted mean, `dismissed_mean',Non-granted complier mean, `complier_mean', complier mean mogstad, `complier_mean_mogstad', dependent_mean, `dependent_mean', individuals, `individuals')





local depvar "employed_pstill unemployed_pstill out_of_lf_pstill timelon_real aktiv_real passiv_real formue_real koejd_real house tilbtot_real disability kontant_real social netovskud_real dispon_ny_real legmarried attrition mortgage_real"





foreach var of varlist `depvar'{

// complier means for non-granted
capture drop y_no_treat
gen y_no_treat= no_treat*`var'

ivreghdfe y_no_treat (no_treat=iv_lawyer2) , absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 
local complier_mean=_b[no_treat]

// complier mean non-granted mogstad

reg `var' lawyer_residual if granted==0

capture drop upper_bound
capture drop lower_bound

gen upper_bound = _b[_cons]+_b[lawyer_residual]*p99_iv_lawyer2
gen lower_bound = _b[_cons] + _b[lawyer_residual]*p1_iv_lawyer2


capture drop complier_mean_mogstad
local complier_mean_mogstad = ((never_taker_lawyer2+ complier_iv_lawyer2)/complier_iv_lawyer2)*(lower_bound)-(never_taker_lawyer2/complier_iv_lawyer2)*upper_bound



sum `var' if granted==0
local dismissed_mean=r(mean)


sum `var' 
local dependent_mean=r(mean)



reg `var' granted `Xvar_short' i.court_pyear if time==1
local individuals=e(N)

ivreghdfe `var' (granted=iv_lawyer2) `Xvar_short' aardum2-aardum35, absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 


outreg2 using iv_results_below_median_wealth.xls, append ctitle(IV `var')  keep(granted) ///
 addstat(Non-granted mean, `dismissed_mean',Non-granted complier mean, `complier_mean', complier mean mogstad, `complier_mean_mogstad', dependent_mean, `dependent_mean', individuals, `individuals')



}





local depvar "bankgaeld_real"





foreach var of varlist `depvar'{

// complier means for non-granted
capture drop y_no_treat
gen y_no_treat= no_treat*`var'

ivreghdfe y_no_treat (no_treat=iv_lawyer2) , absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 
local complier_mean=_b[no_treat]

// complier mean non-granted mogstad

reg `var' lawyer_residual if granted==0

capture drop upper_bound
capture drop lower_bound

gen upper_bound = _b[_cons]+_b[lawyer_residual]*p99_iv_lawyer2
gen lower_bound = _b[_cons] + _b[lawyer_residual]*p1_iv_lawyer2


capture drop complier_mean_mogstad
local complier_mean_mogstad = ((never_taker_lawyer2+ complier_iv_lawyer2)/complier_iv_lawyer2)*(lower_bound)-(never_taker_lawyer2/complier_iv_lawyer2)*upper_bound



sum `var' if granted==0
local dismissed_mean=r(mean)


sum `var' 
local dependent_mean=r(mean)



reg `var' granted `Xvar_short' i.court_pyear if time==1
local individuals=e(N)

ivreghdfe `var' (granted=iv_lawyer2) `Xvar_short'  aardum5-aardum35, absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 


outreg2 using iv_results_below_median_wealth.xls, append ctitle(IV `var')  keep(granted) ///
 addstat(Non-granted mean, `dismissed_mean',Non-granted complier mean, `complier_mean', complier mean mogstad, `complier_mean_mogstad', dependent_mean, `dependent_mean', individuals, `individuals')



}



restore 



* size of debt

* Abobe/below median debt(mean 4 years prior). 

	egen permanent_passiv_groups_prior=xtile(passiv_mean), nq(2) by(pyear)
order passiv_mean permanent_passiv_groups_prior 

*2 above 1 below



preserve 

keep if permanent_passiv_groups_prior==2



local Xvar_short "male age_0_40 age_41_50 age_51_60 age_61_70 erhvervsindk_mean employed_pstill_mean unemployed_pstill_mean legmarried_m1 racedum1 housedum1 passiv_m1 aktiv_m1 higher_secondary university social_mean edu_missing housing_missing timelon_dum1 timelon_dum2 timelon_dum3 timelon_dum4 house_m1"



/* IV Regress */


// Complier mean "Family welfare culture style" NEW


reg granted iv_lawyer2 `Xvar_short' i.court_pyear , cluster(lawyer_id_unique)

reg iv_lawyer2 i.court_pyear if granted==0
predict lawyer_residual, res

reg granted lawyer_residual  i.court_pyear , cluster(lawyer_id_unique)


gen beta_lawyer2=_b[lawyer_residual]

gen alpha_lawyer2=_b[_cons]

sort aar pnr

// Percentiles of instrument

egen p99_iv_lawyer2=pctile(iv_lawyer2), p(99)

egen p1_iv_lawyer2=pctile(iv_lawyer2), p(1)

egen p99_iv_judge2=pctile(iv_lawyer2), p(99)

egen p1_iv_judge2=pctile(iv_lawyer2), p(1)


// IV_lawyer_2

gen complier_iv_lawyer2= beta_lawyer2*(p99_iv_lawyer2-p1_iv_lawyer2) in 1 

gen always_taker_lawyer2 = alpha_lawyer2 + beta_lawyer2 * p1_iv_lawyer2 in 1

gen never_taker_lawyer2 = 1 - alpha_lawyer2 - beta_lawyer2 * p99_iv_lawyer2 in 1

gen all_lawyer2 = complier_iv_lawyer2 + always_taker_lawyer2 + never_taker_lawyer2 in 1

capture drop lawyer_residual

reg iv_lawyer2 i.court_pyear if granted==0
predict lawyer_residual, res

reg erhvervsindk_real lawyer_residual if granted==0

capture drop upper_bound
capture drop lower_bound

gen upper_bound = _b[_cons]+_b[lawyer_residual]*p99_iv_lawyer2
gen lower_bound = _b[_cons] + _b[lawyer_residual]*p1_iv_lawyer2


capture drop complier_mean_mogstad
local complier_mean_mogstad = ((never_taker_lawyer2+ complier_iv_lawyer2)/complier_iv_lawyer2)*(lower_bound)-(never_taker_lawyer2/complier_iv_lawyer2)*upper_bound




// END NEW



// complier means for non-granted

gen no_treat=(1-granted)



gen y_no_treat= no_treat*erhvervsindk_real
gen y_treat= granted*erhvervsindk_real


ivreghdfe y_no_treat (no_treat=iv_lawyer2) , absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) // Include controls? 
local complier_mean=_b[no_treat]

ivreghdfe y_treat (granted=iv_lawyer2) , absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 



sum erhvervsindk_real if granted==0

local dismissed_mean=r(mean)



sum erhvervsindk_real 

local dependent_mean=r(mean)



reg   erhvervsindk_real granted `Xvar_short' i.court_pyear if time==1
local individuals=e(N)



ivreghdfe erhvervsindk_real (granted=iv_lawyer2) `Xvar_short' aardum2-aardum35, absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 




outreg2 using iv_results_above_median_debt.xls, replace ctitle(IV erhvervsindk_real) keep(granted) ///
 addstat(Non-granted mean, `dismissed_mean',Non-granted complier mean, `complier_mean', complier mean mogstad, `complier_mean_mogstad', dependent_mean, `dependent_mean', individuals, `individuals')





local depvar "employed_pstill unemployed_pstill out_of_lf_pstill timelon_real aktiv_real passiv_real formue_real koejd_real house tilbtot_real disability kontant_real social netovskud_real dispon_ny_real legmarried attrition mortgage_real "





foreach var of varlist `depvar'{

// complier means for non-granted
capture drop y_no_treat
gen y_no_treat= no_treat*`var'

ivreghdfe y_no_treat (no_treat=iv_lawyer2) , absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 
local complier_mean=_b[no_treat]

// complier mean non-granted mogstad

reg `var' lawyer_residual if granted==0

capture drop upper_bound
capture drop lower_bound

gen upper_bound = _b[_cons]+_b[lawyer_residual]*p99_iv_lawyer2
gen lower_bound = _b[_cons] + _b[lawyer_residual]*p1_iv_lawyer2


capture drop complier_mean_mogstad
local complier_mean_mogstad = ((never_taker_lawyer2+ complier_iv_lawyer2)/complier_iv_lawyer2)*(lower_bound)-(never_taker_lawyer2/complier_iv_lawyer2)*upper_bound



sum `var' if granted==0
local dismissed_mean=r(mean)


sum `var' 
local dependent_mean=r(mean)



reg `var' granted `Xvar_short' i.court_pyear if time==1
local individuals=e(N)

ivreghdfe `var' (granted=iv_lawyer2) `Xvar_short' aardum2-aardum35, absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 


outreg2 using iv_results_above_median_debt.xls, append ctitle(IV `var')  keep(granted) ///
 addstat(Non-granted mean, `dismissed_mean',Non-granted complier mean, `complier_mean', complier mean mogstad, `complier_mean_mogstad', dependent_mean, `dependent_mean', individuals, `individuals')



}





local depvar "bankgaeld_real"





foreach var of varlist `depvar'{

// complier means for non-granted
capture drop y_no_treat
gen y_no_treat= no_treat*`var'

ivreghdfe y_no_treat (no_treat=iv_lawyer2) , absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 
local complier_mean=_b[no_treat]

// complier mean non-granted mogstad

reg `var' lawyer_residual if granted==0

capture drop upper_bound
capture drop lower_bound

gen upper_bound = _b[_cons]+_b[lawyer_residual]*p99_iv_lawyer2
gen lower_bound = _b[_cons] + _b[lawyer_residual]*p1_iv_lawyer2


capture drop complier_mean_mogstad
local complier_mean_mogstad = ((never_taker_lawyer2+ complier_iv_lawyer2)/complier_iv_lawyer2)*(lower_bound)-(never_taker_lawyer2/complier_iv_lawyer2)*upper_bound



sum `var' if granted==0
local dismissed_mean=r(mean)


sum `var' 
local dependent_mean=r(mean)



reg `var' granted `Xvar_short' i.court_pyear if time==1
local individuals=e(N)

ivreghdfe `var' (granted=iv_lawyer2) `Xvar_short'  aardum5-aardum35, absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 


outreg2 using iv_results_above_median_debt.xls, append ctitle(IV `var')  keep(granted) ///
 addstat(Non-granted mean, `dismissed_mean',Non-granted complier mean, `complier_mean', complier mean mogstad, `complier_mean_mogstad', dependent_mean, `dependent_mean', individuals, `individuals')



}



restore 



* below



preserve 

keep if permanent_passiv_groups_prior==1



local Xvar_short "male age_0_40 age_41_50 age_51_60 age_61_70 erhvervsindk_mean employed_pstill_mean unemployed_pstill_mean legmarried_m1 racedum1 housedum1 passiv_m1 aktiv_m1 higher_secondary university social_mean edu_missing housing_missing timelon_dum1 timelon_dum2 timelon_dum3 timelon_dum4 house_m1"



/* IV Regress */


// Complier mean "Family welfare culture style" NEW


reg granted iv_lawyer2 `Xvar_short' i.court_pyear , cluster(lawyer_id_unique)

reg iv_lawyer2 i.court_pyear if granted==0
predict lawyer_residual, res

reg granted lawyer_residual  i.court_pyear , cluster(lawyer_id_unique)


gen beta_lawyer2=_b[lawyer_residual]

gen alpha_lawyer2=_b[_cons]

sort aar pnr

// Percentiles of instrument

egen p99_iv_lawyer2=pctile(iv_lawyer2), p(99)

egen p1_iv_lawyer2=pctile(iv_lawyer2), p(1)

egen p99_iv_judge2=pctile(iv_lawyer2), p(99)

egen p1_iv_judge2=pctile(iv_lawyer2), p(1)


// IV_lawyer_2

gen complier_iv_lawyer2= beta_lawyer2*(p99_iv_lawyer2-p1_iv_lawyer2) in 1 

gen always_taker_lawyer2 = alpha_lawyer2 + beta_lawyer2 * p1_iv_lawyer2 in 1

gen never_taker_lawyer2 = 1 - alpha_lawyer2 - beta_lawyer2 * p99_iv_lawyer2 in 1

gen all_lawyer2 = complier_iv_lawyer2 + always_taker_lawyer2 + never_taker_lawyer2 in 1

capture drop lawyer_residual

reg iv_lawyer2 i.court_pyear if granted==0
predict lawyer_residual, res

reg erhvervsindk_real lawyer_residual if granted==0

capture drop upper_bound
capture drop lower_bound

gen upper_bound = _b[_cons]+_b[lawyer_residual]*p99_iv_lawyer2
gen lower_bound = _b[_cons] + _b[lawyer_residual]*p1_iv_lawyer2


capture drop complier_mean_mogstad
local complier_mean_mogstad = ((never_taker_lawyer2+ complier_iv_lawyer2)/complier_iv_lawyer2)*(lower_bound)-(never_taker_lawyer2/complier_iv_lawyer2)*upper_bound




// END NEW




// complier means for non-granted

gen no_treat=(1-granted)



gen y_no_treat= no_treat*erhvervsindk_real
gen y_treat= granted*erhvervsindk_real


ivreghdfe y_no_treat (no_treat=iv_lawyer2) , absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) // Include controls? 
local complier_mean=_b[no_treat]

ivreghdfe y_treat (granted=iv_lawyer2) , absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 



sum erhvervsindk_real if granted==0

local dismissed_mean=r(mean)



sum erhvervsindk_real 

local dependent_mean=r(mean)




reg   erhvervsindk_real granted `Xvar_short' i.court_pyear if time==1
local individuals=e(N)



ivreghdfe erhvervsindk_real (granted=iv_lawyer2) `Xvar_short' aardum2-aardum35, absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 





outreg2 using iv_results_below_median_debt.xls, replace ctitle(IV erhvervsindk_real) keep(granted) ///
 addstat(Non-granted mean, `dismissed_mean',Non-granted complier mean, `complier_mean', complier mean mogstad, `complier_mean_mogstad', dependent_mean, `dependent_mean', individuals, `individuals')





local depvar "employed_pstill unemployed_pstill out_of_lf_pstill timelon_real aktiv_real passiv_real formue_real koejd_real house tilbtot_real disability kontant_real social netovskud_real dispon_ny_real legmarried attrition mortgage_real"





foreach var of varlist `depvar'{

// complier means for non-granted
capture drop y_no_treat
gen y_no_treat= no_treat*`var'

ivreghdfe y_no_treat (no_treat=iv_lawyer2) , absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 
local complier_mean=_b[no_treat]

// complier mean non-granted mogstad

reg `var' lawyer_residual if granted==0

capture drop upper_bound
capture drop lower_bound

gen upper_bound = _b[_cons]+_b[lawyer_residual]*p99_iv_lawyer2
gen lower_bound = _b[_cons] + _b[lawyer_residual]*p1_iv_lawyer2


capture drop complier_mean_mogstad
local complier_mean_mogstad = ((never_taker_lawyer2+ complier_iv_lawyer2)/complier_iv_lawyer2)*(lower_bound)-(never_taker_lawyer2/complier_iv_lawyer2)*upper_bound



sum `var' if granted==0
local dismissed_mean=r(mean)


sum `var' 
local dependent_mean=r(mean)



reg `var' granted `Xvar_short' i.court_pyear if time==1
local individuals=e(N)

ivreghdfe `var' (granted=iv_lawyer2) `Xvar_short' aardum2-aardum35, absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 


outreg2 using iv_results_below_median_debt.xls, append ctitle(IV `var')  keep(granted) ///
 addstat(Non-granted mean, `dismissed_mean',Non-granted complier mean, `complier_mean', complier mean mogstad, `complier_mean_mogstad', dependent_mean, `dependent_mean', individuals, `individuals')



}





local depvar "bankgaeld_real"





foreach var of varlist `depvar'{

// complier means for non-granted
capture drop y_no_treat
gen y_no_treat= no_treat*`var'

ivreghdfe y_no_treat (no_treat=iv_lawyer2) , absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 
local complier_mean=_b[no_treat]

// complier mean non-granted mogstad

reg `var' lawyer_residual if granted==0

capture drop upper_bound
capture drop lower_bound

gen upper_bound = _b[_cons]+_b[lawyer_residual]*p99_iv_lawyer2
gen lower_bound = _b[_cons] + _b[lawyer_residual]*p1_iv_lawyer2


capture drop complier_mean_mogstad
local complier_mean_mogstad = ((never_taker_lawyer2+ complier_iv_lawyer2)/complier_iv_lawyer2)*(lower_bound)-(never_taker_lawyer2/complier_iv_lawyer2)*upper_bound



sum `var' if granted==0
local dismissed_mean=r(mean)


sum `var' 
local dependent_mean=r(mean)



reg `var' granted `Xvar_short' i.court_pyear if time==1
local individuals=e(N)

ivreghdfe `var' (granted=iv_lawyer2) `Xvar_short'  aardum5-aardum35, absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 


outreg2 using iv_results_below_median_debt.xls, append ctitle(IV `var')  keep(granted) ///
 addstat(Non-granted mean, `dismissed_mean',Non-granted complier mean, `complier_mean', complier mean mogstad, `complier_mean_mogstad', dependent_mean, `dependent_mean', individuals, `individuals')



}



restore 





* marital status

capture drop married_before
gen married_before=.

replace married_before=1 if legmarried_m1+legmarried_m2+legmarried_m3+legmarried_m4>0 & legmarried_m1+legmarried_m2+legmarried_m3+legmarried_m4!=.

replace married_before=0 if legmarried_m1+legmarried_m2+legmarried_m3+legmarried_m4==0

* married before



preserve 

keep if married_before==1



local Xvar_short "male age_0_40 age_41_50 age_51_60 age_61_70 erhvervsindk_mean employed_pstill_mean unemployed_pstill_mean legmarried_m1 racedum1 housedum1 passiv_m1 aktiv_m1 higher_secondary university social_mean edu_missing housing_missing timelon_dum1 timelon_dum2 timelon_dum3 timelon_dum4 house_m1"



/* IV Regress */


// Complier mean "Family welfare culture style" NEW


reg granted iv_lawyer2 `Xvar_short' i.court_pyear , cluster(lawyer_id_unique)

reg iv_lawyer2 i.court_pyear if granted==0
predict lawyer_residual, res

reg granted lawyer_residual  i.court_pyear , cluster(lawyer_id_unique)


gen beta_lawyer2=_b[lawyer_residual]

gen alpha_lawyer2=_b[_cons]

sort aar pnr

// Percentiles of instrument

egen p99_iv_lawyer2=pctile(iv_lawyer2), p(99)

egen p1_iv_lawyer2=pctile(iv_lawyer2), p(1)

egen p99_iv_judge2=pctile(iv_lawyer2), p(99)

egen p1_iv_judge2=pctile(iv_lawyer2), p(1)


// IV_lawyer_2

gen complier_iv_lawyer2= beta_lawyer2*(p99_iv_lawyer2-p1_iv_lawyer2) in 1 

gen always_taker_lawyer2 = alpha_lawyer2 + beta_lawyer2 * p1_iv_lawyer2 in 1

gen never_taker_lawyer2 = 1 - alpha_lawyer2 - beta_lawyer2 * p99_iv_lawyer2 in 1

gen all_lawyer2 = complier_iv_lawyer2 + always_taker_lawyer2 + never_taker_lawyer2 in 1

capture drop lawyer_residual

reg iv_lawyer2 i.court_pyear if granted==0
predict lawyer_residual, res

reg erhvervsindk_real lawyer_residual if granted==0

capture drop upper_bound
capture drop lower_bound

gen upper_bound = _b[_cons]+_b[lawyer_residual]*p99_iv_lawyer2
gen lower_bound = _b[_cons] + _b[lawyer_residual]*p1_iv_lawyer2


capture drop complier_mean_mogstad
local complier_mean_mogstad = ((never_taker_lawyer2+ complier_iv_lawyer2)/complier_iv_lawyer2)*(lower_bound)-(never_taker_lawyer2/complier_iv_lawyer2)*upper_bound




// END NEW


// complier means for non-granted

gen no_treat=(1-granted)



gen y_no_treat= no_treat*erhvervsindk_real
gen y_treat= granted*erhvervsindk_real


ivreghdfe y_no_treat (no_treat=iv_lawyer2) , absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) // Include controls? 
local complier_mean=_b[no_treat]

ivreghdfe y_treat (granted=iv_lawyer2) , absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 



sum erhvervsindk_real if granted==0

local dismissed_mean=r(mean)



sum erhvervsindk_real 

local dependent_mean=r(mean)




reg   erhvervsindk_real granted `Xvar_short' i.court_pyear if time==1
local individuals=e(N)



ivreghdfe erhvervsindk_real (granted=iv_lawyer2) `Xvar_short' aardum2-aardum35, absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 





outreg2 using iv_results_married.xls, replace ctitle(IV erhvervsindk_real) keep(granted) ///
 addstat(Non-granted mean, `dismissed_mean',Non-granted complier mean, `complier_mean', complier mean mogstad, `complier_mean_mogstad', dependent_mean, `dependent_mean', individuals, `individuals')





local depvar "employed_pstill unemployed_pstill out_of_lf_pstill timelon_real aktiv_real passiv_real formue_real koejd_real house tilbtot_real disability kontant_real social netovskud_real dispon_ny_real legmarried attrition mortgage_real"





foreach var of varlist `depvar'{

// complier means for non-granted
capture drop y_no_treat
gen y_no_treat= no_treat*`var'

ivreghdfe y_no_treat (no_treat=iv_lawyer2) , absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 
local complier_mean=_b[no_treat]

// complier mean non-granted mogstad

reg `var' lawyer_residual if granted==0

capture drop upper_bound
capture drop lower_bound

gen upper_bound = _b[_cons]+_b[lawyer_residual]*p99_iv_lawyer2
gen lower_bound = _b[_cons] + _b[lawyer_residual]*p1_iv_lawyer2


capture drop complier_mean_mogstad
local complier_mean_mogstad = ((never_taker_lawyer2+ complier_iv_lawyer2)/complier_iv_lawyer2)*(lower_bound)-(never_taker_lawyer2/complier_iv_lawyer2)*upper_bound



sum `var' if granted==0
local dismissed_mean=r(mean)


sum `var' 
local dependent_mean=r(mean)



reg `var' granted `Xvar_short' i.court_pyear if time==1
local individuals=e(N)

ivreghdfe `var' (granted=iv_lawyer2) `Xvar_short' aardum2-aardum35, absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 


outreg2 using iv_results_married.xls, append ctitle(IV `var')  keep(granted) ///
 addstat(Non-granted mean, `dismissed_mean',Non-granted complier mean, `complier_mean', complier mean mogstad, `complier_mean_mogstad', dependent_mean, `dependent_mean', individuals, `individuals')



}





local depvar "bankgaeld_real"





foreach var of varlist `depvar'{

// complier means for non-granted
capture drop y_no_treat
gen y_no_treat= no_treat*`var'

ivreghdfe y_no_treat (no_treat=iv_lawyer2) , absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 
local complier_mean=_b[no_treat]

// complier mean non-granted mogstad

reg `var' lawyer_residual if granted==0

capture drop upper_bound
capture drop lower_bound

gen upper_bound = _b[_cons]+_b[lawyer_residual]*p99_iv_lawyer2
gen lower_bound = _b[_cons] + _b[lawyer_residual]*p1_iv_lawyer2


capture drop complier_mean_mogstad
local complier_mean_mogstad = ((never_taker_lawyer2+ complier_iv_lawyer2)/complier_iv_lawyer2)*(lower_bound)-(never_taker_lawyer2/complier_iv_lawyer2)*upper_bound



sum `var' if granted==0
local dismissed_mean=r(mean)


sum `var' 
local dependent_mean=r(mean)



reg `var' granted `Xvar_short' i.court_pyear if time==1
local individuals=e(N)

ivreghdfe `var' (granted=iv_lawyer2) `Xvar_short'  aardum5-aardum35, absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 


outreg2 using iv_results_married.xls, append ctitle(IV `var')  keep(granted) ///
 addstat(Non-granted mean, `dismissed_mean',Non-granted complier mean, `complier_mean', complier mean mogstad, `complier_mean_mogstad', dependent_mean, `dependent_mean', individuals, `individuals')



}



restore 



* not married



preserve 

keep if married_before==0



local Xvar_short "male age_0_40 age_41_50 age_51_60 age_61_70 erhvervsindk_mean employed_pstill_mean unemployed_pstill_mean legmarried_m1 racedum1 housedum1 passiv_m1 aktiv_m1 higher_secondary university social_mean edu_missing housing_missing timelon_dum1 timelon_dum2 timelon_dum3 timelon_dum4 house_m1"



/* IV Regress */


// Complier mean "Family welfare culture style" NEW


reg granted iv_lawyer2 `Xvar_short' i.court_pyear , cluster(lawyer_id_unique)

reg iv_lawyer2 i.court_pyear if granted==0
predict lawyer_residual, res

reg granted lawyer_residual  i.court_pyear , cluster(lawyer_id_unique)


gen beta_lawyer2=_b[lawyer_residual]

gen alpha_lawyer2=_b[_cons]

sort aar pnr

// Percentiles of instrument

egen p99_iv_lawyer2=pctile(iv_lawyer2), p(99)

egen p1_iv_lawyer2=pctile(iv_lawyer2), p(1)

egen p99_iv_judge2=pctile(iv_lawyer2), p(99)

egen p1_iv_judge2=pctile(iv_lawyer2), p(1)


// IV_lawyer_2

gen complier_iv_lawyer2= beta_lawyer2*(p99_iv_lawyer2-p1_iv_lawyer2) in 1 

gen always_taker_lawyer2 = alpha_lawyer2 + beta_lawyer2 * p1_iv_lawyer2 in 1

gen never_taker_lawyer2 = 1 - alpha_lawyer2 - beta_lawyer2 * p99_iv_lawyer2 in 1

gen all_lawyer2 = complier_iv_lawyer2 + always_taker_lawyer2 + never_taker_lawyer2 in 1

capture drop lawyer_residual

reg iv_lawyer2 i.court_pyear if granted==0
predict lawyer_residual, res

reg erhvervsindk_real lawyer_residual if granted==0

capture drop upper_bound
capture drop lower_bound

gen upper_bound = _b[_cons]+_b[lawyer_residual]*p99_iv_lawyer2
gen lower_bound = _b[_cons] + _b[lawyer_residual]*p1_iv_lawyer2


capture drop complier_mean_mogstad
local complier_mean_mogstad = ((never_taker_lawyer2+ complier_iv_lawyer2)/complier_iv_lawyer2)*(lower_bound)-(never_taker_lawyer2/complier_iv_lawyer2)*upper_bound




// END NEW




// complier means for non-granted

gen no_treat=(1-granted)



gen y_no_treat= no_treat*erhvervsindk_real
gen y_treat= granted*erhvervsindk_real


ivreghdfe y_no_treat (no_treat=iv_lawyer2) , absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) // Include controls? 
local complier_mean=_b[no_treat]

ivreghdfe y_treat (granted=iv_lawyer2) , absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 



sum erhvervsindk_real if granted==0

local dismissed_mean=r(mean)



sum erhvervsindk_real 

local dependent_mean=r(mean)




reg   erhvervsindk_real granted `Xvar_short' i.court_pyear if time==1
local individuals=e(N)



ivreghdfe erhvervsindk_real (granted=iv_lawyer2) `Xvar_short' aardum2-aardum35, absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 





outreg2 using iv_results_not_married.xls, replace ctitle(IV erhvervsindk_real) keep(granted) ///
 addstat(Non-granted mean, `dismissed_mean',Non-granted complier mean, `complier_mean', complier mean mogstad, `complier_mean_mogstad', dependent_mean, `dependent_mean', individuals, `individuals')





local depvar "employed_pstill unemployed_pstill out_of_lf_pstill timelon_real aktiv_real passiv_real formue_real koejd_real house tilbtot_real disability kontant_real social netovskud_real dispon_ny_real legmarried attrition mortgage_real"





foreach var of varlist `depvar'{

// complier means for non-granted
capture drop y_no_treat
gen y_no_treat= no_treat*`var'

ivreghdfe y_no_treat (no_treat=iv_lawyer2) , absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 
local complier_mean=_b[no_treat]

// complier mean non-granted mogstad

reg `var' lawyer_residual if granted==0

capture drop upper_bound
capture drop lower_bound

gen upper_bound = _b[_cons]+_b[lawyer_residual]*p99_iv_lawyer2
gen lower_bound = _b[_cons] + _b[lawyer_residual]*p1_iv_lawyer2


capture drop complier_mean_mogstad
local complier_mean_mogstad = ((never_taker_lawyer2+ complier_iv_lawyer2)/complier_iv_lawyer2)*(lower_bound)-(never_taker_lawyer2/complier_iv_lawyer2)*upper_bound



sum `var' if granted==0
local dismissed_mean=r(mean)


sum `var' 
local dependent_mean=r(mean)



reg `var' granted `Xvar_short' i.court_pyear if time==1
local individuals=e(N)

ivreghdfe `var' (granted=iv_lawyer2) `Xvar_short' aardum2-aardum35, absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 


outreg2 using iv_results_not_married.xls, append ctitle(IV `var')  keep(granted) ///
 addstat(Non-granted mean, `dismissed_mean',Non-granted complier mean, `complier_mean', complier mean mogstad, `complier_mean_mogstad', dependent_mean, `dependent_mean', individuals, `individuals')



}





local depvar "bankgaeld_real"





foreach var of varlist `depvar'{

// complier means for non-granted
capture drop y_no_treat
gen y_no_treat= no_treat*`var'

ivreghdfe y_no_treat (no_treat=iv_lawyer2) , absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 
local complier_mean=_b[no_treat]

// complier mean non-granted mogstad

reg `var' lawyer_residual if granted==0

capture drop upper_bound
capture drop lower_bound

gen upper_bound = _b[_cons]+_b[lawyer_residual]*p99_iv_lawyer2
gen lower_bound = _b[_cons] + _b[lawyer_residual]*p1_iv_lawyer2


capture drop complier_mean_mogstad
local complier_mean_mogstad = ((never_taker_lawyer2+ complier_iv_lawyer2)/complier_iv_lawyer2)*(lower_bound)-(never_taker_lawyer2/complier_iv_lawyer2)*upper_bound



sum `var' if granted==0
local dismissed_mean=r(mean)


sum `var' 
local dependent_mean=r(mean)



reg `var' granted `Xvar_short' i.court_pyear if time==1
local individuals=e(N)

ivreghdfe `var' (granted=iv_lawyer2) `Xvar_short'  aardum5-aardum35, absorb(court_pyear) tol(1e-6) first cluster(lawyer_id_unique) 


outreg2 using iv_results_not_married.xls, append ctitle(IV `var')  keep(granted) ///
 addstat(Non-granted mean, `dismissed_mean',Non-granted complier mean, `complier_mean', complier mean mogstad, `complier_mean_mogstad', dependent_mean, `dependent_mean', individuals, `individuals')



}



restore 

